//
//  SearchTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/10/9.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SearchTableViewCell.h"
#import "MarkLabel.h"

@interface SearchTableViewCell ()

// 商品图
@property (strong, nonatomic) UIImageView *goodIV;
// 券图案
@property (strong, nonatomic) UIImageView *vouchIV;
// 券金额
@property (strong, nonatomic) UILabel *vouchMoneyL;
// 标题
@property (strong, nonatomic) UILabel *titleL;
// 优惠价
@property (strong, nonatomic) UILabel *salePriceL;
// 原价
@property (strong, nonatomic) UILabel *originalPriceL;
// 已售
@property (strong, nonatomic) UILabel *sellOutL;

// 券
@property (strong, nonatomic) MarkLabel *ticketL;
// 趣豆抵扣
@property (strong, nonatomic) MarkLabel *integDiscountL;

@property (strong, nonatomic) UIView *lineV;

@end

@implementation SearchTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = WhiteColor;
        
        [self.contentView addSubview:self.goodIV];
        [self.contentView addSubview:self.vouchIV];
        [self.contentView addSubview:self.vouchMoneyL];
        [self.contentView addSubview:self.titleL];
        [self.contentView addSubview:self.sellOutL];
        [self.contentView addSubview:self.salePriceL];
        [self.contentView addSubview:self.originalPriceL];
        [self.contentView addSubview:self.ticketL];
        [self.contentView addSubview:self.integDiscountL];
        [self.contentView addSubview:self.lineV];
        
    }
    
    return self;
    
}

- (void)setSearchModel:(SearchResultModel *)searchModel {
    
    _searchModel = searchModel;
    
    self.titleL.text = searchModel.title;

    NSString *price = [NSString stringWithFormat:@"￥%@", searchModel.zk_final_price];
    NSMutableAttributedString *priceAttributeStr = [[NSMutableAttributedString alloc] initWithString:price];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(30) range:NSMakeRange(1, price.length - 1)];
//    [priceAttributeStr addAttribute:NSExpansionAttributeName value:@0.2 range:NSMakeRange(0, price.length)];
    self.salePriceL.attributedText = priceAttributeStr;
    
    
    NSString *oldPrice = [NSString stringWithFormat:@"￥%@", searchModel.goods_price];
    NSDictionary *attributeDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *oldPirceAttributeStr = [[NSMutableAttributedString alloc] initWithString:oldPrice attributes:attributeDic];
    self.originalPriceL.attributedText = oldPirceAttributeStr;

    
    NSString *str = nil;
    
    if ([searchModel.pict_url hasPrefix:@"http://"] || [searchModel.pict_url hasPrefix:@"https://"]) {
        str = searchModel.pict_url;
    }else {
        str = [NSString stringWithFormat:@"http://%@",searchModel.pict_url];
    }
    
    [self.goodIV sd_setImageWithURL:[NSURL URLWithString:str]];
    
    if ([self isBlankString:searchModel.coupon_info]) {
        [self.integDiscountL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleL.mas_left);
        }];
        self.ticketL.hidden = self.vouchMoneyL.hidden = self.vouchIV.hidden = YES;
        self.vouchMoneyL.font = Font(24);
        self.vouchMoneyL.text = @"";
    } else {
        self.ticketL.hidden = self.vouchMoneyL.hidden = self.vouchIV.hidden = NO;
        [self.integDiscountL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleL.mas_left).offset(20);
        }];
        self.vouchMoneyL.font = Font(24);
        self.vouchMoneyL.text = searchModel.coupon_info;
    }
    
    

//    self.integDiscountL.text = [NSString stringWithFormat:@"趣豆返%@", searchModel.commission_rate];
    
    [self.ticketL sizeToFit];
    [self.integDiscountL sizeToFit];
  
}

- (BOOL)isBlankString:(NSString *)aStr {
    if (!aStr) {
        return YES;
    }
    if ([aStr isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (!aStr.length) {
        return YES;
    }
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmedStr = [aStr stringByTrimmingCharactersInSet:set];
    if (!trimmedStr.length) {
        return YES;
    }
    return NO;
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.goodIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(SmallMargin);
        make.centerY.mas_equalTo(weakSelf.contentView.mas_centerY);
        make.width.mas_equalTo(jisuan(180));
        make.height.mas_equalTo(jisuan(180));
        
    }];
    
    [self.salePriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.titleL.mas_left);
        make.bottom.mas_equalTo(weakSelf.goodIV.mas_bottom).with.offset(-jisuan(10));
    }];
    
    [self.vouchIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-Margin);
        make.bottom.mas_equalTo(weakSelf.salePriceL.mas_bottom);
        make.width.mas_equalTo(jisuan(160));
        make.height.mas_equalTo(jisuan(60));

    }];
    
    [self.vouchMoneyL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.vouchIV.mas_centerY);
        make.right.mas_equalTo(weakSelf.vouchIV.mas_right).with.offset(-jisuan(60));
        make.width.mas_equalTo(jisuan(95));
        make.height.mas_equalTo(jisuan(60));
    }];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.goodIV.mas_right).with.offset(Margin);
        make.top.mas_equalTo(weakSelf.goodIV.mas_top).with.offset(jisuan(10));
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-Margin);
    }];
    
 
    [self.originalPriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.salePriceL.mas_right);
        make.bottom.mas_equalTo(weakSelf.salePriceL.mas_bottom);
    }];

    
    [self.ticketL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.salePriceL.mas_left);
        make.bottom.mas_equalTo(weakSelf.salePriceL.mas_top).with.offset(-jisuan(10));
        
    }];
    
    [self.integDiscountL mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.left.mas_equalTo(weakSelf.ticketL.mas_right).with.offset(jisuan(10));
        make.bottom.mas_equalTo(weakSelf.salePriceL.mas_top).with.offset(-jisuan(10));
        
    }];
    
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left);
        make.right.mas_equalTo(weakSelf.contentView.mas_right);
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
        make.height.mas_equalTo(jisuan(2));
    }];
    
}

#pragma mark -LAZY
- (UIImageView *)goodIV {
    
    if (!_goodIV) {
        
        _goodIV = [[UIImageView alloc] init];
        _goodIV.contentMode = UIViewContentModeScaleAspectFill;
        _goodIV.layer.masksToBounds = YES;
        
        _goodIV.backgroundColor = Colorf2f2f2;
        
    }
    
    return _goodIV;
}

- (UIImageView *)vouchIV {
    
    if (!_vouchIV) {
        
        _vouchIV = [[UIImageView alloc] init];
        _vouchIV.image = [UIImage imageNamed:@"vuochersBG"];
        _vouchIV.contentMode = UIViewContentModeScaleAspectFit;
    
    }
    
    return _vouchIV;
}

- (UILabel *)vouchMoneyL {
    
    if (!_vouchMoneyL) {
        
        _vouchMoneyL = [[UILabel alloc] init];
        _vouchMoneyL.textColor = Colorff0000;
        _vouchMoneyL.textAlignment = NSTextAlignmentCenter;
        _vouchMoneyL.font = Font(24);
        _vouchMoneyL.numberOfLines = 2;
        _vouchMoneyL.lineBreakMode = NSLineBreakByTruncatingTail;

        [_vouchMoneyL sizeToFit];
        
    }
    
    return _vouchMoneyL;
}
- (UILabel *)titleL {
    
    if (!_titleL) {
        
        _titleL = [[UILabel alloc] init];
        _titleL.textAlignment = NSTextAlignmentLeft;
        _titleL.textColor = Color333333;
        _titleL.font = Font(24);
        _titleL.numberOfLines = 2;
        _titleL.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    
    return _titleL;
}

- (UILabel *)salePriceL {
    
    if (!_salePriceL) {
        
        _salePriceL = [[UILabel alloc] init];
        _salePriceL.textAlignment = NSTextAlignmentLeft;
        _salePriceL.textColor = Colorff0000;

    }
    
    return _salePriceL;
}

- (UILabel *)originalPriceL {
    
    if (!_originalPriceL) {
        
        _originalPriceL = [[UILabel alloc] init];
        _originalPriceL.textAlignment = NSTextAlignmentLeft;
        _originalPriceL.textColor = Color999999;
        _originalPriceL.font = Font(20);

    }
    
    return _originalPriceL;
}

- (UILabel *)sellOutL {
    
    if (!_sellOutL) {
        
        _sellOutL = [[UILabel alloc] init];
        _sellOutL.textColor = Color999999;
        _sellOutL.textAlignment = NSTextAlignmentLeft;
        _sellOutL.font = Font(24);
        _sellOutL.text = @"已售：";
    
    }
    
    return  _sellOutL;
}

- (MarkLabel *)ticketL {
    
    if (!_ticketL) {
        
        _ticketL = [[MarkLabel alloc] init];
        _ticketL.textColor = Colorff0000;
        _ticketL.font = Font(20);
        _ticketL.text = @"券";
        _ticketL.edgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
        _ticketL.layer.borderWidth = jisuan(2);
        _ticketL.layer.borderColor = Colorff0000.CGColor;
        _ticketL.layer.cornerRadius = jisuan(10);
        _ticketL.layer.masksToBounds = YES;
        
    }
    
    return _ticketL;
}

- (MarkLabel *)integDiscountL {
    
    if (!_integDiscountL) {
        
        _integDiscountL = [[MarkLabel alloc] init];
        _integDiscountL.textColor = Colorff0000;
        _integDiscountL.font = Font(20);
        
//        _integDiscountL.text = @"趣豆返";
        _integDiscountL.edgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
        _integDiscountL.layer.borderWidth = jisuan(2);
        _integDiscountL.layer.borderColor = Colorff0000.CGColor;
        _integDiscountL.layer.cornerRadius = jisuan(10);
        _integDiscountL.layer.masksToBounds = YES;
        
    }
    
    return _integDiscountL;
}

- (UIView *)lineV {
    
    if (!_lineV) {
        
        _lineV = [[UIView alloc] init];
        _lineV.backgroundColor = Colordddddd;
    }
    return _lineV;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
